package com.fanhao.dllo.justforyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.JokeMessageCommentAdapter;
import com.fanhao.dllo.justforyou.adapter.JokePictureCommentAdapter;
import com.fanhao.dllo.justforyou.entity.JokeCommentData;
import com.fanhao.dllo.justforyou.entity.JokeCommentResult;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/26.
 */
public class JokeMessageCommentActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private JokeMessageCommentAdapter jokeMessageCommentAdapter;
    private RequestQueue queue;

    private ImageView returnIv;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_joke_comment);


        initView();
        initData();
    }

    private void initView() {
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        GridLayoutManager gm = new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(gm);

        jokeMessageCommentAdapter = new JokeMessageCommentAdapter();
        recyclerView.setAdapter(jokeMessageCommentAdapter);

        returnIv = (ImageView)findViewById(R.id.returnIv);
        returnIv.setOnClickListener(this);

        Intent intent = getIntent();
        id = intent.getStringExtra("jokeMesId");

    }

    private void initData() {
        queue = Volley.newRequestQueue(this);
        String url ="http://api.budejie.com/api/api_open.php?market=tencentyingyongbao&udid=8" +
                "66394023055494&a=dataList&appname=baisibudejie&c=comment&os=4.4.4&client=and" +
                "roid&userID=&page=1&per=20&visiting=&data_id="+id+"&lastcid=&hot=1&mac=3c%" +
                "3A91%3A57%3Aeb%3Aee%3Acd&ver=6.1.6";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        JokeCommentResult jcr = gson.fromJson(response,
                                JokeCommentResult.class);
                        if (jcr != null) {
                            List<JokeCommentData> data = jcr.getData();
//                            List<User> users = new ArrayList<>();
//                            for (JokeCommentData jokeCommentData : data) {
//                                User user = jokeCommentData.getUser();
//                                users.add(user);
//
//                            }
                            if (data != null && data.size() > 0) {
                                Log.i("fanhao",data+"坎坎坷坷");
                                jokeMessageCommentAdapter.addData(data);

                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;
            }
        };

        queue.add(request);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.returnIv:
                finish();
                break;
        }

    }
}

